/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package com.moltos.gsonT;

import com.google.gson.Gson;
import com.moltos.parser.ChatParser;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author MoLt1eS
 */
public class ChatConverter {
    

    
    public static void main(String[] args) {
        Gson gson = new Gson();
        File dir = new File("./chat");
        File[] saves = dir.listFiles();
        ChatParser cp = new ChatParser(saves[0]);
        String json = gson.toJson(cp);
        ChatParser cpt;
        cpt = gson.fromJson(json, ChatParser.class);
        System.out.println("JSON String:");
        System.out.println(json);
        System.out.println("Json to Object:");
        System.out.println(cpt.getName());
        System.out.println(cpt.getName_target());
        List<String> sp = cpt.getLines();
        for(int i=0; i<cpt.getnLines();i++){
            System.out.println(sp.get(i));
        }
        
        System.out.println("Saving as File outputstream....");
        
        //Save File
        try {
            File outF = new File("./chat/teste.txt");
            outF.getParentFile().mkdirs();
            FileOutputStream fileOut = new FileOutputStream(outF);
            ObjectOutputStream out = new ObjectOutputStream(fileOut);
            out.writeObject(json);
            out.close();
            
        } catch (IOException ex) {
            Logger.getLogger(ChatConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        System.out.println("Saved on ./chat/teste.txt");
        String jog=null;
        try
        {
           File outF = new File("./chat/teste.txt");
           FileInputStream fileIn = new FileInputStream(outF);
           ObjectInputStream rd = new ObjectInputStream(fileIn);
           jog = (String) rd.readObject();
           rd.close();
           fileIn.close();
        }catch(IOException i)
        {
        }catch(ClassNotFoundException c)
        {
           System.out.println("Player class not found");
        }
        
        cpt = gson.fromJson(jog, ChatParser.class);
        System.out.println(cpt.getName());
        
    }
    
}
